package com.itheima.easy.controller;

import com.itheima.easy.base.PageResponse;
import com.itheima.easy.base.AjaxResult;
import com.itheima.easy.dto.NursingPlanDto;
import com.itheima.easy.service.NursingPlanService;
import com.itheima.easy.vo.NursingPlanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursing")
@Api(tags = "护理计划相关接口")
public class NursingController {
    private final NursingPlanService nursingPlanService;

    @Autowired
    public NursingController(NursingPlanService nursingPlanService) {
        this.nursingPlanService = nursingPlanService;
    }

    @ApiOperation("添加护理计划")
    @PostMapping("/plan")
    public AjaxResult addNursingPlan(@RequestBody NursingPlanDto nursingPlan) {
        nursingPlanService.add(nursingPlan);
        return AjaxResult.success();
    }

    @ApiOperation("修改护理计划")
    @PutMapping("/plan/{id}")
    public AjaxResult updateNursingPlan(@PathVariable Integer id, @RequestBody NursingPlanDto nursingPlan) {
        nursingPlanService.update(nursingPlan);
        return AjaxResult.success();
    }

    @ApiOperation("删除护理计划")
    @DeleteMapping("/plan/{id}")
    public AjaxResult deleteNursingPlan(@PathVariable Long id) {
        nursingPlanService.deleteById(id);
        return AjaxResult.success();
    }

    @ApiOperation("根据id查询护理计划")
    @GetMapping("/plan/{id}")
    public AjaxResult<NursingPlanVo> getNursingPlanById(@PathVariable Long id) {
        return AjaxResult.success(nursingPlanService.getById(id));
    }

    @ApiOperation("查询所有护理计划")
    @GetMapping("/plan")
    public AjaxResult<List<NursingPlanVo>> getAllNursingPlan() {
        return AjaxResult.success(nursingPlanService.listAll());
    }

    @ApiOperation("根据名称和状态分页查询")
    @GetMapping("/plan/search")
    public AjaxResult<PageResponse<NursingPlanVo>> searchNursingPlan(@RequestParam(required = false) String name,
                                                                     @RequestParam(required = false) Integer status,
                                                                     @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return AjaxResult.success(nursingPlanService.listByPage(name, status, pageNum, pageSize));
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation("启用/禁用")
    public AjaxResult enableOrDisable(@PathVariable Long id, @PathVariable Integer status) {
        nursingPlanService.enableOrDisable(id, status);
        return AjaxResult.success();
    }
}
